package com.lord.label.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lord.label.common.Collect;
import com.lord.label.common.PageSize;
import com.lord.label.common.ResultAjax;
import com.lord.label.domain.Category;
import com.lord.label.domain.Result;
import com.lord.label.domain.Rule;
import com.lord.label.service.CategoryService;
import com.lord.label.service.ResultService;
import com.lord.label.service.RuleService;
import com.lord.label.utils.JsoupUtil;
import com.lord.label.utils.ResultUtil;

/**
 * 
 * @Description: 采集结果控制层
 * @author Lord
 * @date 2017年6月1日 上午10:13:58
 */
@Controller
public class ResultController {
	
	@Autowired
	private ResultService resultService;
	
	@Autowired
	private CategoryService categoryService;
	
	@Autowired
	private RuleService ruleService;
	
	
	/**
	 * 
	 * @Title: resultFindOne
	 * @Description: 查看采集结果详情
	 * @param id	主键id
	 * @param model
	 * @return
	 * @author Lord 
	 * @date 2017年6月1日 上午10:14:25
	 */
	@GetMapping(value="results/{id}")
	public String resultFindOne(@PathVariable("id") Long id,Model model){
		Result result = resultService.findById(id);
		Category category = categoryService.findById(result.getCategoryId());
		model.addAttribute("result", result);
		model.addAttribute("category", category);
		return "details";
	}
	
	/**
	 * 
	 * @Title: findAll
	 * @Description: 采集结果列表
	 * @param model
	 * @return
	 * @author Lord 
	 * @date 2017年6月1日 上午10:15:35
	 */
	@GetMapping(value="results")
	public String findAll(Model model){
		Page<?> page = PageHelper.startPage(PageSize.ONE.getSize(), PageSize.TWOTEEN.getSize(), true);
		List<Result> list = resultService.findAll();
		List<Category> categorys = categoryService.findAll();
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", page.getTotal());
		map.put("list", list);
		
		model.addAttribute("map", map);
		model.addAttribute("categorys", categorys);
		return "result";
	}
	
	/**
	 * 
	 * @Title: findPage
	 * @Description: 采集结果列表分页
	 * @param model
	 * @param request
	 * @return
	 * @author Lord 
	 * @date 2017-7-20 下午9:36:09
	 */
	@GetMapping(value="page")
	public String findPage(Model model, HttpServletRequest request){
		String pageSize = request.getParameter("page");
		Page<?> page = PageHelper.startPage(Integer.parseInt(pageSize), PageSize.TWOTEEN.getSize(), true);
		List<Result> list = resultService.findAll();
		List<Category> categorys = categoryService.findAll();
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", page.getTotal());
		map.put("list", list);
		
		model.addAttribute("map", map);
		model.addAttribute("categorys", categorys);
		model.addAttribute("pageSize", pageSize);
		return "result";
	}
	
	/**
	 * 
	 * @Title: resultAdd
	 * @Description: 根据链接地址自动获取正文信息
	 * @param result	采集信息实体
	 * @param bindingResult	springMVC参数验证
	 * @return
	 * @author Lord
	 * @date 2017年6月1日 上午10:16:04
	 */
	@SuppressWarnings("unchecked")
	@PostMapping(value="results")
	@ResponseBody
	public ResultAjax<Result> resultAdd(@Valid Result result, BindingResult bindingResult){
		if(bindingResult.hasErrors()){
			return ResultUtil.error(-3, bindingResult.getFieldError().getDefaultMessage());
		}
		
		//查询采集规则
		Rule rule = ruleService.findBySite(JsoupUtil.getWebSite(result.getUrl()));
		
		//获得请求结果并获得正文内容
		Collect collect = JsoupUtil.JsoupUnit(result.getUrl());
		String centent = JsoupUtil.getContent(collect.getParse(), rule.getTextAttribute());
		
		result.setTitle(collect.getTitle());
		result.setCentent(centent);
		result.setGmtCreate(new Date());
		result.setGmtModified(new Date());
		
		return ResultUtil.success(resultService.insert(result));
	}
	
	/**
	 * 
	 * @Title: resultDelete
	 * @Description: 删除采集结果信息
	 * @param id	主键id
	 * @return
	 * @author Lord 
	 * @date 2017年6月1日 上午10:18:28
	 */
	@SuppressWarnings("unchecked")
	@DeleteMapping(value="results/{id}")
	@ResponseBody
	public ResultAjax<Result> resultDelete(@PathVariable("id") Long id){
		return ResultUtil.success(resultService.delete(id));
	}
}
